<template>
    <div style="width: 100%; height: 100%">
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb-base" style="line-height: 30px">
            <el-breadcrumb-item>
                <font-awesome-icon :icon="['fas', 'home']"></font-awesome-icon>
                院内调度
            </el-breadcrumb-item>
        </el-breadcrumb>

        <div class="main-container">
            <el-tabs v-model="workingStatus" @tab-click="workingStatusChange" type="card" ref="filterTabs">
                <el-tab-pane label="待分配" name="waitingAssign">
                    <span slot="label">
                        <i class="el-icon-date"></i> 待分配
                        <el-badge v-if="waitingAssignCount > 0" :value="waitingAssignCount"
                            class="tab-content-badge"></el-badge>
                    </span>
                </el-tab-pane>
                <el-tab-pane label="已分配" name="assigning">
                    <span slot="label">
                        <i class="el-icon-date"></i> 已分配
                        <el-badge v-if="assigningCount > 0" :value="assigningCount" class="tab-content-badge"></el-badge>
                    </span>
                </el-tab-pane>
                <el-tab-pane label="就诊结束" name="finished">
                    <span slot="label">
                        <i class="el-icon-date"></i> 就诊结束
                        <el-badge v-if="finishedCount > 0" :value="finishedCount" class="tab-content-badge"></el-badge>
                    </span>
                </el-tab-pane>
                <el-tab-pane v-if="false" label="全部" name="allIssues">
                    <span slot="label">
                        <i class="el-icon-date"></i> 全部
                    </span>
                </el-tab-pane>
            </el-tabs>

            <el-form :model="filterForm" label-width="70px">
                <el-row>
                    <el-col :span="4">
                        <el-form-item label="病理编号">
                            <el-input v-model="filterForm.pathologyId" placeholder="请输入病理编号" class="input-form" size="mini">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="申请时间">
                            <el-date-picker v-model="createdRange" class="time-picker-form" size="mini" type="daterange"
                                align="right" unlink-panels range-separator="-" start-placeholder="开始日期"
                                end-placeholder="结束日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item class="el-form-item" label="所属医院">
                            <el-select v-model="filterForm.sliceHospital" class="picker-form" size="mini"
                                @change="getBranchList">
                                <el-option v-for="t in hospitalList" :key="t.id" :label="t.name" :value="t.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="分院名称">
                            <el-select v-model="filterForm.branchName" placeholder="请选择分院" clearable class="picker-form"
                                size="mini">
                                <el-option v-for="item in branchCourtsList" :key="item.name" :label="item.name"
                                    :value="item.name"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <div class="expansion" @click="showExpansion = !showExpansion">
                            <font-awesome-icon v-if="!showExpansion" :icon="['fas', 'angle-down']">
                            </font-awesome-icon>
                            <font-awesome-icon v-else :icon="['fas', 'angle-up']">
                            </font-awesome-icon>
                        </div>
                        <el-button @click="search" type="primary" size="mini" style="margin-left: 15px">
                            搜索
                        </el-button>
                    </el-col>
                </el-row>

                <!--dispatcher扩展筛选-->
                <div v-if="showExpansion">
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="患者姓名">
                                <el-input v-model="filterForm.name" placeholder="请输入患者姓名" class="input-form" size="mini">
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="患者性别">
                                <el-radio-group v-model="filterForm.sex" class="radio-group" size="mini">
                                    <el-radio v-for="sex in sexKindList" :key="sex.id" :label="sex.id" :value="sex.id">
                                        {{ sex.name }}
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="检查类型">
                                <el-select v-model="filterForm.checkCategory" placeholder="请选择检查类型" clearable
                                    class="picker-form" size="mini">
                                    <el-option v-for="item in checkCategoryList" :key="item.id" :label="item.name"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="患者年龄">
                                <el-row>
                                    <el-col :span="10">
                                        <el-input v-model="filterForm.startAge" placeholder="起始年龄" class="input-form"
                                            size="mini"></el-input>
                                    </el-col>
                                    <el-col :span="2" class="link-line">-
                                    </el-col>
                                    <el-col :span="10">
                                        <el-input v-model="filterForm.endAge" placeholder="结束年龄" class="input-form"
                                            size="mini"></el-input>
                                    </el-col>
                                </el-row>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="身份证号">
                                <el-input v-model="filterForm.cardId" placeholder="请输入身份证号" class="input-form"
                                    size="mini"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="送检医生">
                                <el-input v-model="filterForm.sendDoctor" placeholder="请输入送检医生" class="input-form"
                                    size="mini"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="联系电话">
                                <el-input v-model="filterForm.sendDoctorTel" placeholder="请输入联系电话" class="input-form"
                                    size="mini"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="病区号">
                                <el-input v-model="filterForm.wardId" placeholder="请输入病区号" class="input-form"
                                    size="mini"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="取材部位">
                                <el-input v-model="filterForm.sampleBody" placeholder="请输入取材部位" class="input-form"
                                    size="mini"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="取材脏器">
                                <el-select v-model="filterForm.sampleOrganId" placeholder="请选择取材脏器" clearable size="mini"
                                    class="picker-form">
                                    <el-option v-for="(
                                            organ, index
                                        ) in sampleOrganList" :key="index" :label="organ.name" :value="organ.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="床位号">
                                <el-input v-model="filterForm.bedId" placeholder="请输入患者年龄" class="input-form"
                                    size="mini"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="标本名称">
                                <el-input v-model="filterForm.specimenName" placeholder="请输入患者年龄" class="input-form"
                                    size="mini"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="标本类别">
                                <el-select v-model="filterForm.specimenType" placeholder="请选择标本类别" clearable
                                    class="picker-form" size="mini">
                                    <el-option v-for="item in specimenCategoryList" :key="item.id" :label="item.name"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="扫描批次">
                                <el-input v-model="filterForm.biopsyHistoryId" placeholder="请输入扫描批次" class="input-form"
                                    size="mini"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
            </el-form>

            <base-table ref="diagnosisTable" v-loading="!diagnosisTable.isReady" :data="diagnosisTable.data"
                :header="diagnosisTable.header" :operation="diagnosisTable.operation" :pageInfo="diagnosisTable.pageInfo"
                :buttonVisibleFunc="diagnosisTableButtonVisibleFunc" :pageSizes="[5, 10, 15, 20, 150]"
                @handleClick="diagnosisTableButtonClick" @pageJumpTo="diagnosisTablePageJumpTo"
                @sizeChange="diagnosisTableSizeChange">
            </base-table>
        </div>

        <el-dialog :close-on-click-modal="false" title="分配专家" :visible.sync="doctorsDialog.visible" width="90%">
            <base-table v-loading="!doctorsTable.isReady" ref="doctorsTable" :data="doctorsTable.data"
                :header="doctorsTable.header" :operation="doctorsTable.operation" :pageInfo="doctorsTable.pageInfo"
                @handleClick="doctorsTableButtonClick" @pageJumpTo="doctorsTablePageJumpTo"
                @sizeChange="doctorsTableSizeChange">
            </base-table>
            <el-row style="margin-top: 10px" type="flex" justify="end">
                <el-button @click="doctorsDialog.visible = false" size="mini">取消
                </el-button>
            </el-row>
        </el-dialog>
    </div>
</template>

<script>
import BaseTable from '@templates/components/BaseTable';
import moment from 'moment/moment';
import _ from 'lodash';
import { itemsPerPage } from '@/client/libs/util';

export default {
    name: 'DiagnosisDispatch',
    components: {
        BaseTable,
    },
    data() {
        return {
            // 所需字典对应表
            dictRequestMap: [
                {
                    code: 'checkCategory',
                    arrayKey: 'checkCategoryList',
                    // }, {
                    //   code: 'patientCategory',
                    //   arrayKey: 'patientCategoryList',
                },
                {
                    code: 'SexKind',
                    arrayKey: 'sexKindList',
                    // }, {
                    //   code: 'ageUnitKind',
                    //   arrayKey: 'ageUnitKindList',
                },
                {
                    code: 'specimenCategory',
                    arrayKey: 'specimenCategoryList',
                },
                {
                    code: 'hospitalList',
                    arrayKey: 'hospitalList',
                },
            ],
            hospitalList: [],
            checkCategoryList: [],
            sexKindList: [],
            specimenCategoryList: [],

            // 取材脏器列表
            sampleOrganList: [],
            // 标签页
            workingStatus: 'waitingAssign',
            // 标签页小红点
            waitingAssignCount: 0,
            assigningCount: 0,
            finishedCount: 0,
            tabCountRequestList: [
                {
                    url: '/diagnosis/wait-dispatch',
                    countKey: 'waitingAssignCount',
                },
                {
                    url: '/diagnosis/tasks',
                    countKey: 'assigningCount',
                },
                {
                    url: '/diagnosis/finished',
                    countKey: 'finishedCount',
                },
            ],

            showExpansion: false,

            diagnosisTable: {
                isReady: false,
                data: [],
                header: [],
                headerPreAssign: [
                    {
                        label: '分院名称',
                        prop: 'branchName',
                        width: '107',
                    },
                    {
                        label: '病理编号',
                        prop: 'pathologyId',
                        width: '107',
                    },
                    {
                        label: '状态',
                        prop: 'currentLevelLabel',
                        width: '80',
                    },
                    {
                        label: '姓名',
                        prop: 'name',
                        width: '80',
                    },
                    {
                        label: '性别',
                        prop: 'sexName',
                        width: '80',
                    },
                    {
                        label: '年龄',
                        prop: 'ageData',
                        width: '80',
                    },
                    {
                        label: '患者类别',
                        prop: 'patientCategoryName',
                        width: '107',
                    },
                    {
                        label: '身份证号',
                        prop: 'cardId',
                        width: '165',
                    },
                    {
                        label: '病区名称',
                        prop: 'wardName',
                        width: '107',
                    },
                    {
                        label: '床位号',
                        prop: 'bedId',
                        width: '107',
                    },
                    {
                        label: '冰冻类型',
                        prop: 'freezeTypeName',
                        width: '107',
                    },
                    {
                        label: '标本类型',
                        prop: 'specimenTypeName',
                        width: '107',
                    },
                    {
                        label: '检查类型',
                        prop: 'checkCategoryName',
                        width: '107',
                    },
                    {
                        label: '诊断组',
                        prop: 'subspecialtyTypeName',
                        width: '107',
                    },
                    {
                        label: '取材部位',
                        prop: 'sampleBody',
                        width: '107',
                    },
                    {
                        label: '取材脏器',
                        prop: 'sampleOrganName',
                        width: '107',
                    },
                    {
                        label: '申请时间',
                        prop: 'createTime',
                        width: '136',
                    },
                    {
                        label: '扫描日期',
                        width: '136',
                        isRender: true,
                        render: (h, params) => {
                            let biopsyScanTimeValue = '';
                            if (params.row.biopsyScanTimeInfoList) {
                                params.row.biopsyScanTimeInfoList.forEach(
                                    (item) => {
                                        if (item.biopsyScanTime == null) {
                                        } else {
                                            biopsyScanTimeValue =
                                                biopsyScanTimeValue +
                                                item.biopsyScanTime +
                                                ' ';
                                        }
                                    }
                                );
                            }
                            return h('span', biopsyScanTimeValue);
                        },
                    },
                    {
                        label: '扫描批次',
                        width: '107',
                        isRender: true,
                        render: (h, params) => {
                            let scanBatchValue = '';
                            if (params.row.biopsyHistoryIdInfoList) {
                                params.row.biopsyHistoryIdInfoList.forEach(
                                    (item) => {
                                        if (item.biopsyHistoryId == null) {
                                        } else {
                                            scanBatchValue =
                                                scanBatchValue +
                                                item.biopsyHistoryId +
                                                ' ';
                                        }
                                    }
                                );
                            }
                            return h('span', scanBatchValue);
                        },
                    },
                ],
                headerAssign: [
                    {
                        label: '分院名称',
                        prop: 'branchName',
                        width: '107',
                    },
                    {
                        label: '病理编号',
                        prop: 'pathologyId',
                        width: '107',
                    },
                    {
                        label: '状态',
                        prop: 'currentLevelLabel',
                        width: '80',
                    },
                    {
                        label: '姓名',
                        prop: 'name',
                        width: '80',
                    },
                    {
                        label: '性别',
                        prop: 'sexName',
                        width: '80',
                    },
                    {
                        label: '年龄',
                        prop: 'ageData',
                        width: '80',
                    },
                    {
                        label: '分配医生',
                        prop: 'assignDoctorName',
                        width: '107',
                    },
                    {
                        label: '初诊医生',
                        prop: 'primaryDoctorName',
                        width: '107',
                    },
                    {
                        label: '审核医生',
                        prop: 'reviewDoctorName',
                        width: '107',
                    },
                    {
                        label: '终审医生',
                        prop: 'finalDoctorName',
                        width: '107',
                    },
                    {
                        label: '患者类别',
                        prop: 'patientCategoryName',
                        width: '107',
                    },
                    {
                        label: '身份证号',
                        prop: 'cardId',
                        width: '165',
                    },
                    {
                        label: '病区名称',
                        prop: 'wardName',
                        width: '107',
                    },
                    {
                        label: '床位号',
                        prop: 'bedId',
                        width: '107',
                    },
                    {
                        label: '取材部位',
                        prop: 'sampleBody',
                        width: '107',
                    },
                    {
                        label: '取材脏器',
                        prop: 'sampleOrganName',
                        width: '107',
                    },
                    {
                        label: '申请时间',
                        prop: 'createTime',
                        width: '136',
                    },
                    {
                        label: '扫描日期',
                        width: '136',
                        isRender: true,
                        render: (h, params) => {
                            let biopsyScanTimeValue = '';
                            if (params.row.biopsyScanTimeInfoList) {
                                params.row.biopsyScanTimeInfoList.forEach(
                                    (item) => {
                                        if (item.biopsyScanTime == null) {
                                        } else {
                                            biopsyScanTimeValue =
                                                biopsyScanTimeValue +
                                                item.biopsyScanTime +
                                                ' ';
                                        }
                                    }
                                );
                            }
                            return h('span', biopsyScanTimeValue);
                        },
                    },
                    {
                        label: '扫描批次',
                        width: '107',
                        isRender: true,
                        render: (h, params) => {
                            let scanBatchValue = '';
                            if (params.row.biopsyHistoryIdInfoList) {
                                params.row.biopsyHistoryIdInfoList.forEach(
                                    (item) => {
                                        if (item.biopsyHistoryId == null) {
                                        } else {
                                            scanBatchValue =
                                                scanBatchValue +
                                                item.biopsyHistoryId +
                                                ' ';
                                        }
                                    }
                                );
                            }
                            return h('span', scanBatchValue);
                        },
                    },
                ],
                operation: [
                    {
                        label: '分配',
                        functionKey: 'diagnosisAssign',
                    },
                    {
                        label: '详情',
                        functionKey: 'diagnosisDetail',
                    },
                    {
                        label: '召回',
                        color: 'red',
                        functionKey: 'diagnosisCallback',
                    },
                    {
                        label: '重新分配',
                        color: 'red',
                        functionKey: 'diagnosisReassign',
                    },
                ],
                pageInfo: {
                    page: 1,
                    size: itemsPerPage.get(),
                    total: 0,
                },
            },

            doctorsTable: {
                isReady: false,
                data: [],
                header: [
                    {
                        label: '姓名',
                        prop: 'name',
                        width: '80',
                    },
                    {
                        label: '医生职称',
                        prop: 'positionName',
                        width: '150',
                    },
                    {
                        label: '医生职级',
                        prop: 'medicalTitleName',
                        width: '150',
                    },
                    {
                        label: '所属医院',
                        prop: 'institutionName',
                    },
                    {
                        label: '医生擅长',
                        prop: 'major',
                    },
                ],
                operation: [
                    {
                        label: '分配',
                        functionKey: 'doctorsTableAssign',
                    },
                ],
                pageInfo: {
                    page: 1,
                    size: itemsPerPage.get(),
                    total: 0,
                },
            },
            currentRowDocTor: '',
            diagnosisID: null,

            doctorsDialog: {
                visible: false,
            },

            createdRange: [],
            filterForm: {
                pathologyId: '',
                name: '',
                sampleOrganId: '', // 取材脏器
                sendDoctor: '', // 送检医生
                checkCategory: '', // 检查类型
                startAge: '',
                endAge: '',
                cardId: '',
                sex: '',
                wardId: '',
                bedId: '',
                sampleBody: '', // 取材部位
                sendDoctorTel: '', // 联系电话
                specimenName: '', // 标本名称
                specimenType: '', // 标本类别
                scanDateEnd: '', // 最小创建时间
                scanDateStart: '', // 最大创建时间
                biopsyHistoryId: '', //扫描批次
            },

            branchCourtsList: [], //分院列表
            filterTemp: {},
        };
    },
    mounted() {
        // 获取字典数据
        this.dictionariesRequest();
        // 获取脏器数据
        this.sampleOrganRequest();
        this.diagnosisTableRequest();
    },
    computed: {
        diagnosisTableUrl() {
            if (this.workingStatus === 'waitingAssign') {
                this.diagnosisTable.header =
                    this.diagnosisTable.headerPreAssign;
                return 'wait-dispatch';
            }
            if (this.workingStatus === 'assigning') {
                this.diagnosisTable.header = this.diagnosisTable.headerAssign;
                return 'tasks';
            }
            if (this.workingStatus === 'finished') {
                this.diagnosisTable.header = this.diagnosisTable.headerAssign;
                return 'finished';
            }
            if (this.workingStatus === 'allIssues') {
                return '';
            }
            return '';
        },
    },
    methods: {
        //获取分院列表
        getBranchList(id) {
            this.branchCourtsList = []
            let mData = this.hospitalList.filter((item) => item.id === id)[0]
            if (mData.firstCode) {
                mData.firstCode.split('、').forEach((item) => {
                    this.branchCourtsList.push({ name: item })
                })
            }
        },
        // 请求字典信息
        dictionariesRequest() {
            this.dictRequestMap.forEach((map) => {
                this.$axios({
                    method: 'GET',
                    url: '/PROXY-Remote/dicts/list',
                    params: {
                        code: map.code,
                        page: 0,
                        size: 99,
                    },
                }).then((res) => {
                    if (res && res.data.code === 200) {
                        this[map.arrayKey] = _.clone(res.data.data);

                        if (map.code === 'SexKind') {
                            this[map.arrayKey].push({
                                id: '',
                                name: '全部',
                            });
                        }
                    }
                });
            });
        },
        // 请求脏器信息
        sampleOrganRequest() {
            this.$axios({
                // 详见 axios 请求配置项
                method: 'GET',
                url: '/PROXY-Remote/organs',
                params: {
                    size: 10000,
                },
            }).then((res) => {
                if (res && res.data.code === 200) {
                    res.data.data.forEach((t) => {
                        this.sampleOrganList.push({
                            id: t.id,
                            name: t.name,
                        });
                    });
                }
            });
        },

        workingStatusChange() {
            // 重置页码
            this.diagnosisTable.pageInfo.page = 1;
            this.diagnosisTableRequest();
        },
        // 数量统计
        diagnosisCountRequest() {
            this.tabCountRequestList.forEach((t) => {
                this.$axios({
                    url: `/PROXY-Remote${t.url}`,
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    params: {
                        isShowDispatcher: 1,
                    },
                    data: {
                        size: 10,
                        page: 0,
                    },
                }).then((res) => {
                    if (res && res.data.code === 200) {
                        const result = res.data;
                        this[t.countKey] = result.pageInfo.total;
                    } else {
                        throw new Error(res.data);
                    }
                });
            });
        },

        // 对年龄进行判断
        ageValidate() {
            // 判断年龄格式
            if (this.filterForm.startAge) {
                const age = _.toNumber(this.filterForm.startAge);
                if (typeof age !== 'number' || Number.isNaN(age)) {
                    this.$message.warning('年龄必须为数字！');
                    return false;
                }
            }
            if (this.filterForm.endAge) {
                const age = _.toNumber(this.filterForm.endAge);
                if (typeof age !== 'number' || Number.isNaN(age)) {
                    this.$message.warning('年龄必须为数字！');
                    return false;
                }
            }
            // 判断年龄范围
            if (this.filterForm.startAge && this.filterForm.endAge) {
                if (this.filterForm.startAge >= this.filterForm.endAge) {
                    this.$message.warning('起始年龄应该小于结束年龄!');
                    return false;
                }
            }
            return true;
        },

        search() {
            if (!this.ageValidate()) {
                return false;
            }
            // 确定时间范围
            if (this.createdRange && this.createdRange.length > 0) {
                let createTimeStart = '';
                let createTimeEnd = '';
                [createTimeStart, createTimeEnd] = this.createdRange;
                this.filterForm.scanDateEnd = moment(createTimeEnd)
                    .format('YYYY-MM-DD hh:mm:ss')
                    .replace(' ', ' ');
                this.filterForm.scanDateStart = moment(createTimeStart)
                    .format('YYYY-MM-DD hh:mm:ss')
                    .replace(' ', ' ');
            }
            // 保存过滤条件到 Temp
            this.filterTemp = {};
            Object.keys(this.filterForm).forEach((key) => {
                if (this.filterForm[key]) {
                    this.filterTemp[key] = this.filterForm[key];
                }
            });
            // 重置页码
            this.diagnosisTable.pageInfo.page = 1;
            this.diagnosisTableRequest();
            return true;
        },

        // 诊断数据获取
        async diagnosisTableRequest() {
            this.diagnosisTable.isReady = false;
            this.diagnosisCountRequest();

            // 过滤条件为已保存的 formTemp 中获取
            this.filterTemp.size = this.diagnosisTable.pageInfo.size;
            this.filterTemp.page = this.diagnosisTable.pageInfo.page - 1;

            const res = await this.$axios({
                method: 'POST',
                url: `/PROXY-Remote/diagnosis/${this.diagnosisTableUrl}`,
                params: {
                    isShowDispatcher: 1,
                },
                data: this.filterTemp,
            });

            if (res && res.data.code === 200) {
                // 请求成功
                const result = res.data;
                // 分页信息
                this.diagnosisTable.pageInfo = {
                    total: result.pageInfo.total,
                    page: result.pageInfo.page + 1,
                    size: result.pageInfo.size,
                };

                // 查询结果数据
                this.diagnosisTable.data = [];
                let _this = this;
                if (result.data) {
                    result.data.forEach((t, index) => {
                        const item = {
                            index,
                            currentLevel: t.currentLevel,
                            remark: t.remark,
                            id: t.id,
                            pathologyId: t.pathologyId,
                            currentLevelLabel: t.currentLevelLabel,
                            name: t.name,
                            sexName:
                                t.sex === 282
                                    ? '未知'
                                    : t.sex === 283
                                        ? '男'
                                        : t.sex === 284
                                            ? '女'
                                            : t.sex === 285
                                                ? '女性变男性'
                                                : t.sex === 286
                                                    ? '男性变女性'
                                                    : '未说明',
                            ageData: t.age,
                            patientCategoryName: t.patientCategoryName,
                            cardId: t.cardId ? t.cardId : '',
                            wardName: t.wardName,
                            bedId: t.bedId,
                            sampleBody: t.sampleBody,
                            sampleOrganName: t.sampleOrganName,
                            biopsyHistoryId: t.biopsyHistoryId,
                            biopsyCreateTime: t.biopsyCreateTime,
                            freezeTypeName: t.freezeTypeName,
                            specimenTypeName: t.specimenTypeName,
                            checkCategoryName: t.checkCategoryName,
                            subspecialtyTypeName: t.subspecialtyTypeName,
                            sampleBody: t.sampleBody,
                            scanDefinition: t.scanDefinition,
                            biopsyQuality: t.biopsyQuality,
                            biopsyScanTimeInfoList: t.biopsyScanTimeInfoList,
                            biopsyHistoryIdInfoList: t.biopsyHistoryIdInfoList,
                            branchName: t.branchName,
                            createTime: moment(t.createTime)
                                .format('YYYY-MM-DD hh:mm:ss')
                                .replace(' ', '\n'),
                            assignDoctorName: _this.getDocterNameByLevel(2, t),
                            primaryDoctorName: _this.getDocterNameByLevel(2, t),
                            reviewDoctorName: _this.getDocterNameByLevel(4, t),
                            finalDoctorName: _this.getDocterNameByLevel(5, t),
                        };

                        this.diagnosisTable.data.push(item);
                    });
                }
            } else {
                this.$message.warning('请求失败');
            }

            this.diagnosisTable.isReady = true;
        },
        //根据阶段获取医生姓名
        getDocterNameByLevel(level, item) {
            let mDocterName = '';
            for (let i = 0; i < item.diagnosisDetailList.length; i++) {
                if (level == item.diagnosisDetailList[i].currentLevel) {
                    mDocterName = item.diagnosisDetailList[i].doctorName;
                    break;
                }
            }
            return mDocterName;
        },
        diagnosisTableButtonVisibleFunc(functionKey, row) {
            switch (functionKey) {
                case 'diagnosisAssign':
                    return row.currentLevel === 1;
                case 'diagnosisReassign':
                    return row.currentLevel !== 1 && row.currentLevel !== 6;
                case 'diagnosisCallback':
                    return row.currentLevel !== 1 && row.currentLevel !== 6;
                default:
                    return true;
            }
        },
        diagnosisTablePageJumpTo(val) {
            this.diagnosisTable.pageInfo.page = val;
            this.diagnosisTableRequest();
        },
        diagnosisTableSizeChange(val) {
            this.diagnosisTable.pageInfo.size = val;
            this.diagnosisTableRequest();
        },
        diagnosisTableButtonClick(functionKey, row) {
            this[functionKey](row);
        },
        diagnosisAssign(row) {
            this.diagnosisID = row.id;
            this.doctorsDialogOpen();
        },
        diagnosisDetail(row) {
            this.$store.commit('setPropertyGeneral', {
                key: 'diagnosis',
                value: {
                    id: row.id,
                    pathologyId: row.pathologyId,
                },
            });
            this.$router.push('/diagnosis/dispatch/detail');
            //   将pathologyId存入sessionStorage中
            sessionStorage.setItem('pathologyId', row.pathologyId);
        },
        diagnosisReassign(row) {
            this.$prompt(
                `是否重新分配病理编号为：${row.pathologyId} 的诊断？请输入重新分配说明`,
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /.*[^\s]/,
                    inputErrorMessage: '召回说明不能为空',
                }
            )
                .then(async ({ value }) => {
                    this.diagnosisTable.isReady = false;
                    const res = await this.$axios({
                        method: 'PUT',
                        url: `/PROXY-Remote/diagnosis/${row.id}/recovery`,
                        params: {
                            remark: value,
                        },
                    });

                    if (res && res.data.code === 200) {
                        this.$message.success('召回成功');
                        this.diagnosisAssign(row);
                        this.diagnosisTableRequest();
                    } else {
                        this.$message.warning('召回失败');
                        this.diagnosisTable.isReady = true;
                    }
                })
                .catch(() => { });
        },
        diagnosisCallback(row) {
            this.$prompt(
                `是否召回病理编号为：${row.pathologyId} 的诊断？请输入召回说明`,
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /.*[^\s]/,
                    inputErrorMessage: '召回说明不能为空',
                }
            )
                .then(async ({ value }) => {
                    this.diagnosisTable.isReady = false;
                    const res = await this.$axios({
                        method: 'PUT',
                        url: `/PROXY-Remote/diagnosis/${row.id}/recovery`,
                        params: {
                            isReportOperation: 0,
                            remark: value,
                        },
                    });

                    if (res && res.data.code === 200) {
                        this.$message.success('召回成功');
                        this.diagnosisTableRequest();
                    } else {
                        this.$message.warning('召回失败');
                        this.diagnosisTable.isReady = true;
                    }
                })
                .catch(() => { });
        },

        // 分配医生对话框 - 打开
        doctorsDialogOpen() {
            this.doctorsTable.pageInfo.page = 1;
            this.doctorsDialog.visible = true;
            this.doctorsTableDataGet();
        },
        // 获取分配专家
        async doctorsTableDataGet() {
            const id = this.diagnosisID;
            this.doctorsTable.isReady = false;

            const res = await this.$axios({
                method: 'get',
                url: `/PROXY-Remote/diagnosis/${id}/next-flow-doctors`,
                params: {
                    page: this.doctorsTable.pageInfo.page - 1,
                    size: this.doctorsTable.pageInfo.size,
                },
            });

            if (res && res.data.code === 200) {
                const result = res.data;
                this.doctorsTable.pageInfo = {
                    total: result.pageInfo.total,
                    page: result.pageInfo.page + 1,
                    size: result.pageInfo.size,
                };
                // this.doctorsTable.pageInfo.hide = true;

                this.doctorsTable.data = [];
                result.data.forEach((element) => {
                    this.doctorsTable.data.push(element);
                });
                this.doctorsTable.isReady = true;
            } else {
                this.$message.warning('请求失败');
                this.doctorsTable.isReady = true;
            }
        },
        doctorsTablePageJumpTo(val) {
            this.doctorsTable.pageInfo.page = val;
            this.doctorsTableDataGet();
        },
        doctorsTableSizeChange(val) {
            this.doctorsTable.pageInfo.size = val;
            this.doctorsTableDataGet();
        },
        doctorsTableButtonClick(functionKey, row) {
            this[functionKey](row);
        },
        doctorsTableAssign(currentRowDocTor) {
            this.$confirm(`分配给${currentRowDocTor.name}`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(async () => {
                    const level = _.get(currentRowDocTor, 'nextFlowLevel', 0);
                    if (!level) {
                        this.$message.warning('分配失败');
                        return;
                    }

                    const res = await this.$axios({
                        method: 'put',
                        url: `/PROXY-Remote/diagnosis/${this.diagnosisID}/next-flow`,
                        data: {
                            doctorId: currentRowDocTor.id,
                            isTempStorage: 0,
                            level,
                        },
                        headers: {
                            'Content-Type': 'application/json',
                        },
                    });

                    if (res && res.data.code === 200) {
                        if (this.workingStatus === 'assigning') {
                            this.$message.info('重新分配成功');
                        } else {
                            this.$message.info('分配成功');
                        }
                        this.doctorsDialog.visible = false;
                        this.diagnosisTableRequest();
                    } else if (this.workingStatus === 'assigning') {
                        this.$message.info('重新分配失败');
                    } else {
                        this.$message.warning('分配失败');
                    }
                })
                .catch(() => { });
        },
    },
};
</script>

<style scoped>
.tab-content-badge {
    top: 3px;
}

.expansion {
    width: 30px;
    height: 30px;
    display: inline;
    font-size: 28px;
    line-height: 30px;
    margin-top: 25px;
    vertical-align: middle;
    color: #10a1df;
    cursor: pointer;
}

.link-line {
    text-align: center;
}
</style>
